package com.doge.walknovel.business.reader.download

import com.doge.walknovel.utils.log
import com.liulishuo.okdownload.DownloadListener
import com.liulishuo.okdownload.DownloadTask
import com.liulishuo.okdownload.core.breakpoint.BreakpointInfo
import com.liulishuo.okdownload.core.cause.EndCause
import com.liulishuo.okdownload.core.cause.ResumeFailedCause
import kotlin.math.max
import kotlin.math.min

class DownloadListenerAdapter(
    private val fileName: String,
    private val completedCallback: (EndCause) -> Unit,
) :
    DownloadListener {
    private var received = 0L
    private var total = 0L

    override fun taskStart(task: DownloadTask) {
        log(" DownloadListenerAdapter taskStart $fileName ${task.id}")
        received = 0
    }

    override fun downloadFromBeginning(
        task: DownloadTask,
        info: BreakpointInfo,
        cause: ResumeFailedCause
    ) {
        log(" DownloadListenerAdapter downloadFromBeginning $fileName ${task.id} $info $cause")
        total = info.totalLength
    }

    override fun fetchProgress(task: DownloadTask, blockIndex: Int, increaseBytes: Long) {
        log(" DownloadListenerAdapter fetchProgress $fileName ${task.id} $blockIndex $increaseBytes")
        received += increaseBytes

        if (total > 0) {
            val progress = min(max(received * 1f / total, 0.01f), 1f)
            log(" DownloadListenerAdapter fetchProgress $progress $received/$total")
        }
    }

    override fun downloadFromBreakpoint(task: DownloadTask, info: BreakpointInfo) {
        log(" DownloadListenerAdapter downloadFromBreakpoint $fileName ${task.id}")
    }

    override fun connectEnd(
        task: DownloadTask,
        blockIndex: Int,
        responseCode: Int,
        responseHeaderFields: MutableMap<String, MutableList<String>>
    ) {
        log(" DownloadListenerAdapter connectEnd $fileName ${task.id} $blockIndex $responseCode")
    }

    override fun connectStart(
        task: DownloadTask,
        blockIndex: Int,
        requestHeaderFields: MutableMap<String, MutableList<String>>
    ) {
        log(" DownloadListenerAdapter connectStart $fileName ${task.id} $blockIndex")
    }

    override fun taskEnd(task: DownloadTask, cause: EndCause, realCause: Exception?) {
        log(" DownloadListenerAdapter taskEnd $fileName ${task.id} $cause ${realCause?.message}")
        completedCallback(cause)
    }

    override fun connectTrialEnd(
        task: DownloadTask,
        responseCode: Int,
        responseHeaderFields: MutableMap<String, MutableList<String>>
    ) {
        log(" DownloadListenerAdapter connectTrialEnd $fileName ${task.id} $responseCode")
    }

    override fun fetchStart(task: DownloadTask, blockIndex: Int, contentLength: Long) {
        log(" DownloadListenerAdapter fetchStart $fileName ${task.id} $blockIndex $contentLength")
    }

    override fun fetchEnd(task: DownloadTask, blockIndex: Int, contentLength: Long) {
        log(" DownloadListenerAdapter fetchEnd $fileName ${task.id} $blockIndex $contentLength")
    }

    override fun connectTrialStart(
        task: DownloadTask,
        requestHeaderFields: MutableMap<String, MutableList<String>>
    ) {
        log(" DownloadListenerAdapter connectTrialStart $fileName ${task.id}")
    }
}